Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel

Class BorderDrawer
Private m_BorderColor As Color = Color.Black

Private Const WM_NCPAINT As Integer = &H85
Private WM_ERASEBKGND As Integer = &H14
Private Const WM_PAINT As Integer = &HF
'DCX_CLIPSIBLINGS -> 0x00000010L
Private Const DCX_CLIPSIBLINGS As Integer = 16
'DCX_CACHE -> 0x00000002L
Private DCX_CACHE As Integer = 2
'DCX_WINDOW -> 0x00000001L
Private Const DCX_WINDOW As Integer = 1

Public Sub DrawBorder(ByRef msg As Message, ByVal width As Integer, ByVal height As Integer)

  If msg.Msg = WM_NCPAINT Or msg.Msg = WM_ERASEBKGND Or msg.Msg = WM_PAINT Then
    'Dim hdc As Integer = GetDCEx(msg.HWnd, 1, 1 Or &H20)--> Only work in Win2k. 
    Dim hdc As Integer = GetDCEx(msg.HWnd, 0, DCX_WINDOW Or DCX_CACHE Or DCX_CLIPSIBLINGS)

    If hdc <> 0 Then
      Dim graphics As Graphics = graphics.FromHdc(hdc)
      Dim rectangle As Rectangle = New Rectangle(0, 0, width, height)
      ControlPaint.DrawBorder(graphics, rectangle, m_BorderColor, ButtonBorderStyle.Solid)
      msg.Result = 1
      ReleaseDC(msg.HWnd, hdc)
    End If

  End If
End Sub
<CategoryAttribute("Appearance"), DescriptionAttribute("Border color.")> _
Public Property BorderColor() As Color
  Get
    Return m_BorderColor
  End Get
  Set(ByVal Value As Color)
    m_BorderColor = Value
  End Set
End Property

Declare Function GetDCEx Lib "user32" (ByVal hwnd As Integer, ByVal hrgnclip As Integer, ByVal fdwOptions As Integer) As Integer
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Integer, ByVal hDC As Integer) As Integer
End Class
